package com.android.projetihm.activity.main;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.ViewGroup;
import android.widget.ImageButton;

/**
 * use to download images with performance, for listview
 * 
 * @author Adrien
 *
 */
public class ImageDownloader
{
	//++++++++++++//
	// ATTRIBUTES //
	//++++++++++++//
	
	ViewGroup mImagesContainer;
	

	//++++++++++++++//
	// CONSTRUCTORS //
	//++++++++++++++//
	
	public ImageDownloader(ViewGroup imagesContainer)
	{
		this.mImagesContainer = imagesContainer;
	}
	

	//+++++++++//
	// METHODS //
	//+++++++++//
	
	/**
	 * fill the image view with the good image
	 * @param url
	 * @param imageView the image view
	 */
	public void download(String url)
	{
		BitmapDownloaderTask task =
				new BitmapDownloaderTask();
		task.execute(url);
	}
	
	
	//+++++++++++++++//
	// INNER CLASSES //
	//+++++++++++++++//
	
	/**
	 * permit to download bitmap in imageview
	 * @author Adrien
	 *
	 */
	private class BitmapDownloaderTask
			extends AsyncTask<String, Void, Bitmap>
	{
		@Override
		protected void onPreExecute()
		{
		}
		
		@Override
		protected Bitmap doInBackground(String... params)
		{
			int resId = Integer.parseInt(params[0]);
			Bitmap bitmap = BitmapFactory.decodeResource(
					ImageDownloader.this.mImagesContainer.getResources(),
					resId
					);
			return bitmap;
		}
		
		@Override
		protected void onPostExecute(Bitmap result)
		{
        	ImageButton mImageView = new ImageButton(ImageDownloader.this.mImagesContainer.getContext());
        	ImageDownloader.this.mImagesContainer.addView(mImageView);
			mImageView.setImageBitmap(result);
		}
	}
	
}
